<template>
  <div class="my-box">
    <!-- 未登录 -->
    <!-- $store.state.user 如果未登录那么本地的值就为false -->
    <div class="not-login two-boxbg" v-if="!user">
      <img src="../../assets/mobile.png" @click="$router.push('/login')" alt="">
      <span>登录/注册</span>
    </div>
    <!-- 已登录 -->
    <div v-else class="login-bg two-boxbg">
      <!-- 用户登录信息 -->
      <div class="user-info">
        <div class="user-container">
          <div class="left">
            <van-image round class="user-img" src="https://img01.yzcdn.cn/vant/cat.jpeg" />
            <span class="username">修改资料</span>
          </div>
          <van-button type="default" size="mini" round>默认按钮</van-button>
        </div>

      </div>
      <!-- 用户数据 -->
      <div class="data">
        <div class="data-item">
          <span>{{userDate.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userDate.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userDate.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{userDate.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <!-- 宫格导航 -->
    <van-grid class="nav-grid" :column-num="2">
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text" class="grid-text">收藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text" class="grid-text">历史</span>
      </van-grid-item>
    </van-grid>
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <!-- $store.state.user 如果是登录就为true的状态 -->
    <van-cell @click="login_edit" v-if="user" class="logout-cell" title="退出登录" center />
  </div>

</template>

<script>
import { mapState } from 'vuex'
import { getUserData } from '@/api/users.js'
export default {
  name: 'profile',
  data () {
    return {
      userDate: {}
    }
  },
  created () {
    if (this.user) {
      this.loadData()
    }
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    // 加载数据
    async loadData () {
      try {
        // 获取成功
        const { data: res } = await getUserData()
        this.userDate = res.data
        console.log(this.userDate)
      } catch (err) {
        this.$toast('获取失败')
      }
    },
    // 点击退出登录
    login_edit () {
      this.$dialog.confirm({
        title: '标题',
        message: '弹窗内容'
      })
        .then(() => {
          // on confirm
          this.$store.commit('userData', null)
        })
        .catch(() => {
          // on cancel
        })
    }
  }
}
</script>

<style lang="less" scoped>
// 共同背景
.two-boxbg {
  width: 750px;
  height: 400px;
  background-image: url(~@/assets/banner.png);
  background-size: 100%;
}
// 未登录
.my-box {
  .not-login {
    display: flex;
    // 默认主轴居中
    justify-content: center;
    // 侧轴居中
    align-items: center;
    // 改变主轴方向
    flex-direction: column;
    img {
      height: 132px;
      width: 132px;
    }
    span {
      font-size: 28px;
      color: #fff;
      margin-top: 15px;
    }
  }
}
// 已登录
.login-bg {
  // 用户头像区域
  .user-info {
    height: 247px;
    padding: 0 32px;
    // background-color: pink;
    display: flex;
    align-items: flex-end;
    .user-container {
      height: 132px;
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .left {
      display: flex;
      align-items: center;

      .user-img {
        height: 132px;
        width: 132px;
      }
      .username {
        font-size: 30px;
        color: #fff;
        margin-left: 22px;
      }
    }
  }
  // 用户数据区域
  .data {
    display: flex;
    justify-content: space-around;
    .data-item {
      height: 130px;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      color: #fff;
      span:nth-child(1) {
        font-size: 36px;
        margin-bottom: 15px;
      }
      span:nth-child(2) {
        font-size: 23px;
      }
    }
  }
}
// 宫格
.nav-grid {
  background: #fff;
  .toutiao {
    font-size: 45px;
  }
  .toutiao-shoucang {
    color: #eb5253;
  }
  .toutiao-lishi {
    color: #ff9d1d;
  }
  .text {
    color: #000;
    margin-top: 8px;
    font-size: 28px;
  }
}
// 单元格
.logout-cell {
  height: 100px;
  text-align: center;
  color: #eb5253;
  margin: 10px 0;
  font-size: 30px;
}
</style>
